package com.tao.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

@Data
@AllArgsConstructor
@Component
@NoArgsConstructor
public class JWTUtil {
    private String secretKey ;
    private long ttl;
    private String tokenName;

    public  String createJWT(Map<String,Object> map){
        String jwt=Jwts.builder()
                .signWith(SignatureAlgorithm.HS256,secretKey)
                .setClaims(map)
                .setExpiration(new Date(System.currentTimeMillis() + ttl))
                .compact();
        return jwt;
    }

    public  Claims parseJWT( String jwt){
        Claims claims = Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(jwt)
                .getBody();
        return claims;
    }


}
